package com.example.lncd.model;

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.JsonAdapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Location {
    private Long id;
    private Long userId;
    private double latitude;
    private double longitude;
    
    @SerializedName("timestamp")
    private String timestampStr;
    
    private transient Date timestamp;
    
    private String address;
    
    // 用于日期格式化的静态格式器
    private static final SimpleDateFormat ISO8601_FORMAT;
    private static final SimpleDateFormat SERVER_FORMAT;
    
    static {
        ISO8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        ISO8601_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
        
        SERVER_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    }

    public Location() {
    }

    public Location(Long userId, double latitude, double longitude, Date timestamp) {
        this.userId = userId;
        this.latitude = latitude;
        this.longitude = longitude;
        this.timestamp = timestamp;
        this.timestampStr = ISO8601_FORMAT.format(timestamp);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public Date getTimestamp() {
        if (timestamp == null && timestampStr != null) {
            try {
                // 尝试用服务器格式解析
                timestamp = SERVER_FORMAT.parse(timestampStr);
            } catch (ParseException e) {
                try {
                    // 如果失败，尝试ISO格式
                    timestamp = ISO8601_FORMAT.parse(timestampStr);
                } catch (ParseException ex) {
                    // 无法解析，返回当前时间
                    timestamp = new Date();
                }
            }
        }
        return timestamp != null ? timestamp : new Date();
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
        if (timestamp != null) {
            this.timestampStr = ISO8601_FORMAT.format(timestamp);
        }
    }
    
    public String getTimestampStr() {
        return timestampStr;
    }
    
    public void setTimestampStr(String timestampStr) {
        this.timestampStr = timestampStr;
    }
    
    // 获取ISO8601格式的日期字符串
    public String getFormattedTimestamp() {
        if (getTimestamp() == null) {
            return null;
        }
        return ISO8601_FORMAT.format(getTimestamp());
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
} 